var DxfParser = require('../');
var fs = require('fs');
var path = require('path');

var DXF_FILE_PATH = path.join(__dirname, 'data', 'tg.dxf');
var OUTPUT_FILE_NAME = "out.json";

var fileStream = fs.createReadStream(DXF_FILE_PATH, { encoding: 'utf8' });

var parser = new DxfParser();
var res_struct = {},max = {x:0,y:0},min = {}
//var layers = ['ASSIST','corrs','DXG','DLDW','TKDMTZ','GCD','GXYZ','ntk','SXSS','TK','wtk','ZBTZ']
var layers = ['0','7','Defpoints','DLSS','FWX','JMD','KZD','Store','Store_Mark','text','ZDK','ZJ']
parser.parseStream(fileStream, function(err, dxf) {
    if(err) return console.error(err.stack);
    //fs.writeFileSync(OUTPUT_FILE_NAME, JSON.stringify(dxf, null, 3));
   
    //resetData(dxf)
    var ret = []
    console.log(dxf.entities)
    for(var i in dxf.blocks){
        var enty = dxf.blocks[i]
        console.log(enty.name)
        if(enty.position){
            var res = {}
                res[enty.name] = enty.position
                if(enty.entities)
                res["chr"] = resetData(enty.entities)
                ret.push(res)
        }
        
    }

    fs.writeFileSync('posion.json', JSON.stringify(ret));
});

function resetData(arr){
    var ret = []
    arr.map(function(e){
        {
            if(e.type == 'INSERT'){
                var res = {}
                res[e.name] = e.position
                if(e.entities)
                res["chr"] = resetData(e.entities)
                ret.push(res)
            }
        }
    })
    return ret
}
function getBlockEntity(en,data){
    var res = []

    switch(en.type){
        case 'DIMENSION':
            if(!en.view && en.position){
                en.view = en.position
            }
            console.log(en.block)
            // for(var i = 0;i < data.blocks[en.block].entities.length;i++){
            //     data.blocks[en.block].entities[i].view = en.view
            //     res = res.concat(getBlockEntity(data.blocks[en.block].entities[i],data))
            // }
            break;
        case 'INSERT':
            if(!en.view && en.position){
                en.view = en.position
            }
            for(var i = 0;i < data.blocks[en.name].entities.length;i++){
                if(en.view){
                    data.blocks[en.name].entities[i].view = en.view
                }
                res = res.concat(getBlockEntity(data.blocks[en.name].entities[i],data))
            }
            break;
        default:
            if(layers.indexOf(en.layer) >= 0){
                if(!res_struct[en.type]){
                    res_struct[en.type] = []
                }
                if(en.view){
                    if(en.vertices){
                        en.vertices = en.vertices.map(function(e){
                            if(e.x < 10000){
                                e.x += en.view.x
                                e.y += en.view.y
                                e.z += en.view.z
                            }
                            return e
                        })
                    }
                    else if(en.startPoint ){
                        if(en.startPoint.x < 10000){
                        en.startPoint.x += en.view.x
                        en.startPoint.y += en.view.y
                        en.startPoint.z += en.view.z
                        }
                    }
                    else if(en.position && en.type == "POINT"){
                        if(en.position.x < 10000){
                        en.position.x += en.view.x
                        en.position.y += en.view.y
                        en.position.z += en.view.z
                        }
                    }
                    else if(en.center){
                        if(en.center.x < 10000){
                        en.center.x += en.view.x
                        en.center.y += en.view.y
                        en.center.z += en.view.z
                        }
                    }
                }
                res_struct[en.type].push(en)
                res.push(en)
            }
            break;
    }
    return res
}